package org.dei.perla.sys.device.fpc.dspic;

import java.util.ArrayList;
import java.util.Arrays;

import org.dei.perla.parser.expressions.ConstantBlob;
import org.dei.perla.parser.expressions.ConstantID;
import org.dei.perla.parser.expressions.ConstantInteger;
import org.dei.perla.parser.expressions.ConstantTimestamp;
import org.dei.perla.sys.device.fpc.dspic.datastructures.DspicDataBufferOnEvent;
import org.dei.perla.sys.device.fpc.dspic.datastructures.ED;
import org.dei.perla.sys.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.llqruntime.datastructures.Record;

public class DBRecordUtil {
	
	public static ArrayList<Record> prepareEventRecords(DspicDataBufferOnEvent status, QueryRecordStructure onEventRecordStructure, int pDevNumber){
		if(status.getEventDataCount() == 0)
			return null;
		
		ED eventData;
		ArrayList<Record> arrayList = new ArrayList<Record>();
		for (int i = 0; i < status.getEventDataCount(); i ++) {
			// Inserisco i dati dell'accellerometro
			eventData = status.getEventData()[i];
			Record record = new Record(onEventRecordStructure);
			record.setField("ACQUISITION_TYPE", new ConstantInteger(DSPicFPC.ON_EVENT));
			record.setField("ID_NODO", new ConstantID(pDevNumber));
			record.setField("TIMESTAMP", new ConstantTimestamp(eventData.getTimestamp()));
			record.setField("FLAGS", new ConstantInteger(eventData.getFlags()));
			record.setField("SAMPLES", new ConstantInteger(eventData.getSamples()));
			record.setField("MAGNITUDEX", new ConstantInteger(eventData.getMagnitudeX()));
			record.setField("MAGNITUDEY", new ConstantInteger(eventData.getMagnitudeY()));
			record.setField("MAGNITUDEZ", new ConstantInteger(eventData.getMagnitudeZ()));
			record.setField("NOISEX", new ConstantInteger(eventData.getNoiseX()));
			record.setField("NOISEY", new ConstantInteger(eventData.getNoiseY()));
			record.setField("NOISEZ", new ConstantInteger(eventData.getNoiseZ()));
			record.setField("PEAKX", new ConstantInteger(eventData.getPeakX()));
			record.setField("PEAKY", new ConstantInteger(eventData.getPeakY()));
			record.setField("PEAKZ", new ConstantInteger(eventData.getPeakZ()));
			record.setField("DATA", new ConstantBlob(Arrays.copyOfRange(status.getEventDataSamples(), 2 * eventData.getStart(), 2 * eventData.getStop())));
			arrayList.add(record);
		}
		return arrayList;
	}

	public static Record prepareLowSamplingRateRecords(DspicDataBufferOnEvent status, QueryRecordStructure recordStructure, int pDevNumber){
		// Creo il record ed inserisco i dati a campionamento lento
		Record record = new Record(recordStructure);
		long now = System.currentTimeMillis();
		record.setField("ACQUISITION_TYPE", new ConstantInteger(DSPicFPC.CONTINUOUS));
		record.setField("ID_NODO", new ConstantID(pDevNumber));
		if(status.getEventDataCount() == 0)
			record.setField("TIMESTAMP", new ConstantTimestamp(now));
		else{
			record.setField("TIMESTAMP", new ConstantTimestamp(status.getEventData()[0].getTimestamp()));
		}
		record.setField("TEMPERATURE", new ConstantInteger(status.getLowSamplingRateData().getTemperature()));
		record.setField("ATTITUDEX", new ConstantInteger(status.getLowSamplingRateData().getAttitudeX()));
		record.setField("ATTITUDEY", new ConstantInteger(status.getLowSamplingRateData().getAttitudeY()));
		record.setField("ATTITUDEZ", new ConstantInteger(status.getLowSamplingRateData().getAttitudeZ()));
		record.setField("CRACKMETER1", new ConstantInteger(status.getLowSamplingRateData().getCrackMeter1()));
		record.setField("CRACKMETER2", new ConstantInteger(status.getLowSamplingRateData().getCrackMeter2()));
		return record;
	}
	
	public static QueryRecordStructure createContinuousRecordStructure(){
		QueryRecordStructure continuousRecordStructure = new QueryRecordStructure();
		continuousRecordStructure.addFieldStructure("ID_NODO", ConstantID.class);
		continuousRecordStructure.addFieldStructure("ACQUISITION_TYPE", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("TIMESTAMP", ConstantTimestamp.class);
		continuousRecordStructure.addFieldStructure("TEMPERATURE", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("ATTITUDEX", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("ATTITUDEY", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("ATTITUDEZ", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("CRACKMETER1", ConstantInteger.class);
		continuousRecordStructure.addFieldStructure("CRACKMETER2", ConstantInteger.class);
		return continuousRecordStructure;
	}
	
	public static QueryRecordStructure createOnEventRecordStructure(){
		QueryRecordStructure onEventRecordStructure = new QueryRecordStructure();
		onEventRecordStructure.addFieldStructure("ID_NODO", ConstantID.class);
		onEventRecordStructure.addFieldStructure("ACQUISITION_TYPE", ConstantInteger.class);
		
		onEventRecordStructure.addFieldStructure("NUM_EVENTS", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("FLAGS", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("SAMPLES", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("MAGNITUDEX", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("MAGNITUDEY", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("MAGNITUDEZ", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("PEAKX", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("PEAKY", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("PEAKZ", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("NOISEX", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("NOISEY", ConstantInteger.class);
		onEventRecordStructure.addFieldStructure("NOISEZ", ConstantInteger.class);
		
		onEventRecordStructure.addFieldStructure("DATA", ConstantBlob.class);
		onEventRecordStructure.addFieldStructure("TIMESTAMP", ConstantTimestamp.class);
		return onEventRecordStructure;
		
	}
	
	
}
